package cn.edu.sdjzu.xg.eagle.dao;

import cn.edu.sdjzu.xg.eagle.Service.UserDaoService;
import cn.edu.sdjzu.xg.eagle.entity.User;
import cn.edu.sdjzu.xg.eagle.exception.BaseException;
import cn.edu.sdjzu.xg.eagle.util.JdbcHelper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.HashSet;

public class UserDaoImpl implements UserDaoService {

    private static UserDaoImpl userDaoImpl = new UserDaoImpl();

    private UserDaoImpl() {}

    public static UserDaoImpl getInstance() {
        return userDaoImpl;
    }

    @Override
    public User findUser(Long id) throws SQLException, BaseException {
        Connection connection = JdbcHelper.getConn();
        String sql = "select * from user where id = ?";
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setLong(1, id);
        ResultSet rs = ps.executeQuery();
        if(rs.next()) {
            return new User(
                    rs.getLong("id"),
                    rs.getString("username"),
                    rs.getString("password"),
                    null
            );
        }
        JdbcHelper.close(connection);
        return null;
    }

    @Override
    public Collection<User> findAllUsers() throws SQLException, BaseException {
        String sql = "select * from user";
        Connection connection = JdbcHelper.getConn();
        PreparedStatement ps = connection.prepareStatement(sql);
        ResultSet rs = ps.executeQuery();
        Collection<User> users = new HashSet<User>();
        while(rs.next()) {
            users.add(new User(
                    rs.getLong("id"),
                    rs.getString("username"),
                    rs.getString("password"),
                    null
            ));
        }
        JdbcHelper.close(connection);
        return null;
    }

    @Override
    public boolean addUser(User user, Connection connection) throws SQLException, BaseException {
        String sql = "INSERT INTO user(username,password,teacher_id) Values (?,?,?)";
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setString(1, user.getUsername());
        ps.setString(2, user.getPassword());
        ps.setLong(3, user.getTeacher().getId());

        int rowsAffected = ps.executeUpdate();
        return rowsAffected > 0;
    }

    @Override
    public boolean deleteUser(Long id, Connection connection) throws SQLException, BaseException {
        String sql = "delete from user where id = ?";
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setLong(1, id);
        int rowsAffected = ps.executeUpdate();
        return rowsAffected > 0;
    }

    @Override
    public void login(Connection connection, User user) throws SQLException, BaseException {
        String sql = "SELECT id from user WHERE username = ? AND password = ?";

        try (PreparedStatement ps = connection.prepareStatement(sql)) {
            ps.setString(1, user.getUsername());
            ps.setString(2, user.getPassword());
            ResultSet rs = ps.executeQuery();
            if(rs.next()) {
                user.setId(rs.getLong("id"));
            }
        }
    }
}
